#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
#define MAXV 1010

char laby[MAXV][MAXV];
int n,m;
int firstx,firsty,ans;
int d[MAXV][MAXV];
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};

void bfs(){
	queue<int> q;
	int i,j,tx,ty,vx,vy,hx,hy,max;
	ans = 0;
	memset(d,-1,sizeof(d));
	q.push(firstx);
	q.push(firsty);
	d[firstx][firsty] = 0;
	while(!q.empty()){
		tx = q.front();q.pop();
		ty = q.front();q.pop();
		for(i=0;i<4;i++){
			vx = tx + dx[i],vy = ty + dy[i];
			if(vx>=0 && vx<m && vy>=0 && vy<n && laby[vx][vy] == '.' && d[vx][vy] == -1){
				q.push(vx),q.push(vy);
				d[vx][vy] = d[tx][ty] + 1;
				if(ans < d[vx][vy]){
					ans = d[vx][vy];
					firstx = vx;
					firsty = vy;
				}
			}
		}
	}
}

int main(){
	int i,j,flag,N;
	cin >> N;
	while(N--){
		cin  >> n >> m;
		for(i=0;i<m;i++)
		    cin >> laby[i];
		flag = 1;
		for(i=0;i<m;i++){
		    for(j=0;j<n;j++){
		        if(laby[i][j]=='.'){
					firstx = i,firsty = j, flag =0;
					break;
				}
			}
		}
		bfs();
		bfs();
		cout<<"Maximum rope length is "<< ans << "."<<endl;
	}
	return 0;
}
